package com.hoshiibuy.sys.permission.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.sys.permission.entity.SysRoles;
import com.hoshiibuy.sys.permission.entity.param.SysRolesInsertParam;
import com.hoshiibuy.sys.permission.entity.param.SysRolesQueryParam;
import com.hoshiibuy.sys.permission.entity.param.SysRolesUpdateParam;
import com.hoshiibuy.sys.permission.entity.vo.SysRolesVO;
import com.hoshiibuy.sys.permission.service.impl.SysRolesServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 角色控制器
 * 
 * @author Mr.wu
 *
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping(value = "/role")
public class RoleController {

	@Autowired
	private SysRolesServiceImpl sysRolesService;

	@ApiOperation("新增角色")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addRole(@Valid SysRolesInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysRoles entity = new SysRoles();
		BeanUtils.copyProperties(param, entity);

		// TODO 参数过滤：只接受指定参数
		return sysRolesService.addRole(entity);

	}

	@ApiOperation("修改角色")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateRole(@Valid SysRolesUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysRoles entity = new SysRoles();
		BeanUtils.copyProperties(param, entity);
		// TODO 参数过滤：只接受指定参数
		entity.setUpdateTime(LocalDateTime.now());
		entity.setVersion(entity.getVersion() + 1);
		return sysRolesService.updateRole(entity);
	}

	@ApiOperation("删除角色")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteRole(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysRolesService.deleteRole(id);
	}

	@ApiOperation("获取一个角色的详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysRolesVO> getRoleDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		// TODO 只返回需要的参数
		return sysRolesService.getRoleDetail(id);
	}

	@ApiOperation("获取角色列表")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysRolesVO>> getRoleList(@Valid SysRolesQueryParam param, BindingResult bindingResult)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysRoles> queryWrapper = param.getQueryWrapper();
		String sqlSelect = BeanUtils.getSqlSelect(SysRolesVO.class);
		queryWrapper.select(sqlSelect);
		return sysRolesService.getRoleList(queryWrapper);
		// TODO 只返回需要的参数
	}

	@ApiOperation("分页查询角色")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<SysRolesVO>> getRolePage(@Valid SysRolesQueryParam param, BindingResult bindingResult)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysRoles> queryWrapper = param.getQueryWrapper();
		String sqlSelect = BeanUtils.getSqlSelect(SysRolesVO.class);
		queryWrapper.select(sqlSelect);
		IPage<SysRoles> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

		return sysRolesService.getPage(page, queryWrapper);
	}

}
